package net.xdclass.service.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.component.SmsComponent;
import net.xdclass.config.SmsConfig;
import net.xdclass.constant.RedisKey;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.service.NotifyService;
import net.xdclass.util.CheckUtil;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * 功能描述
 *
 * @author ASUS
 * @date 2024/07/05  0:04
 */
@Service
@Slf4j
public class NotifyServiceImpl implements NotifyService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SmsComponent smsComponent;
    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private StringRedisTemplate redisTemplate;

    //过期时间为10分钟(1秒= 1000毫秒)
    private static final int CODE_EXPIRED = 60*1000*10;
    @Override
    @Async("threadPoolTaskExecutor")
    public void testSend() {
        ResponseEntity<String> forEntity = restTemplate.getForEntity("https://www.663ys.org/xibu/179672.html", String.class);
        String body = forEntity.getBody();
        log.info(body);
    }

    @Override
    public JsonData sendCode(SendCodeEnum sendCodeEnum, String to) {
        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY, sendCodeEnum.name(), to);
        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        if(StringUtils.isNotBlank(cacheValue)){
            String ttl = cacheValue.split("_")[1];
            long leftTime = CommonUtil.getCurrentTimestamp() - Long.parseLong(ttl);
            if(leftTime<1000*60){
                log.info("重复发送短信验证码，时间间隔:{} 秒",leftTime);
                return JsonData.buildResult(BizCodeEnum.CODE_LIMITED);
            }
        }
        String code = CommonUtil.getRandomCode(6);
        String value = code + "_" + CommonUtil.getCurrentTimestamp();
        redisTemplate.opsForValue().set(cacheKey,value,CODE_EXPIRED, TimeUnit.MILLISECONDS);

        if(CheckUtil.isEmail(to)){
            //发送邮箱
        }else if(CheckUtil.isPhone(to)){
            //发到手机上
//            String randomCode = CommonUtil.getRandomCode(6);
            smsComponent.send(to,smsConfig.getTemplateId(),code);

        }else{
            return JsonData.buildResult(BizCodeEnum.CODE_TO_ERROR);
        }
        return JsonData.buildSuccess();
    }

    @Override
    public boolean checkCode(SendCodeEnum sendCodeEnum, String to,String code) {
        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY, sendCodeEnum.name(), to);
        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        if(StringUtils.isNotBlank(cacheValue)){
            String cacheCode = cacheValue.split("_")[0];
            if(cacheCode.equalsIgnoreCase(code)){
                redisTemplate.delete(cacheKey);
                return true;
            }
        }
        return false;
    }
}
